# -*- coding: utf-8 -*-
# !/usr/bin/env python
import uuid

from rest_framework.views import APIView
from components.models import HostsManagement
from components.resource_management.host_manage.dao import query_host_info
from utils.views import Result

class HostManage(APIView):
    def get(self, request):
        data = request.query_params
        host_data_list = []
        if data['host_id']:
            host_data_list = query_host_info(data['host_id'])
        return Result.success_response(data=host_data_list, msg='查询成功')

    def post(self, request):
        data = request.data
        if 'host_id' in data: data.pop('host_id')
        if 'software_data' in data: data.pop('software_data')
        data['status'] = 1
        data['resource_user_ids'] = str(uuid.uuid1())
        try:
            save_data = HostsManagement(**data)
            save_data.save()
        except Exception as e:
            return Result.fail_response(msg=str(e))

        return Result.success_response(msg='保存成功')

    def put(self, request):
        data = request.data.copy()

        HostsManagement.objects.filter(host_id=data['host_id']).update(
            host_ip=data['host_ip'],
            host_nick=data['host_nick'],
            host_connection_code=data['host_connection_code'],
            host_connection_port=data['host_connection_port'],
            host_users_id=data['host_users_id'],
            env_id=data['env_id'],
            host_base_code=data['host_base_code'],
            host_function_code=data['host_function_code'],
            comment=data['comment']
        )

        return Result.success_response(msg='修改成功')


    def delete(self, request):
        data = request.data
        host_ids = data['host_ids']

        HostsManagement.objects.filter(host_id__in=host_ids).update(status=0)

        return Result.success_response(msg='删除成功')